#include <stdio.h>
#include <stdlib.h>

#define TRUE            1
#define FALSE           0

//单向链表
struct tongyu{
  int data;
  struct tongyu *next;
};

typedef struct tongyu tongyu;

int input(int* x0,int* mod,int* yinziA,int* yinziC)
{
    printf("请输入X0,Mod,因子A,因子C的值(中间以空格为分隔符)：\n");

    scanf("%d %d %d %d",x0,mod,yinziA,yinziC);

    if( (*mod) <= 0  )
    {
        printf("输入错误（mod的值小于零)");
        return FALSE;
    }
    if( (*yinziA) < 0 || (*yinziC) < 0 || (*yinziA) > (*mod) || (*yinziC) > (*mod))
    {
        printf("输入错误（yinziA/yinziC的值小于零或大于Mod)");
        return FALSE;
    }
    return TRUE;
}


int main()
{
    int x0, mod, yinziA, yinziC, panduan0 = 0;
    tongyu *h = NULL, *p = NULL, *q = NULL, *panduan1 = NULL;

    if( !input(&x0, &mod, &yinziA, &yinziC) )//判断输入
    {
        exit(EXIT_FAILURE);
    }

    h = (tongyu *)malloc(sizeof(tongyu));
    h->data = x0;
    q = h;
    while( 1 )
    {
        p = (tongyu *)malloc(sizeof(tongyu));
        p->data = ( yinziA * q->data + yinziC ) % mod;
        panduan1 = h;
        while( panduan1 != NULL )
        {
            if( p->data == panduan1->data )
            {
                panduan0 = 1;
                break;
            }
            panduan1 = panduan1->next;
        }
        q->next = p;
        if( panduan0 )
        {
            break;
        }
        q = p;
        q->next = NULL;
    }

    p = h;
    
    //开始遍历
    printf("{ ");
    while( p != NULL)
    {
        printf("%d ",p->data);
        p = p->next;
    }
    printf("}.");

    return 0;
}
